<template>
  <div class="container">
    <div class="package-card bg-white hd-shadow">

      <van-row>
        <van-search
                v-model="value"
                placeholder="请输入搜索关键词"
                show-action
                @search="onSearch"
        >
          <div slot="action" @click="onSearch">搜索</div>
        </van-search>

      </van-row>

      <van-tree-select
              :items="item"
              :main-active-index="mainActiveIndex"
              :active-id="activeId"
              @navclick="onNavClick"
              @itemclick="onItemClick"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'community_affairs',
  data () {
    return {
      value: '',
      newlist: [],
      item: [],
      // 左侧高亮元素的index
      mainActiveIndex: 0,
      // 被选中元素的id
      activeId: 1
    }
  },
  methods: {
    init (search) {
      this.$http({
        fun: 'auditItemService',
        title: search,
        cb: res => {
          // console.log(res)
          this.newlist = res
          let keyss = []
          for (var i = 0; i < this.newlist.length; i++) {
            if (this.newlist[i].datalist !== undefined) {
              var newlist_ = this.newlist[i].datalist
              for (var j = 0; j < newlist_.length; j++) {
                let obj = {
                  text: this.newlist[i].name,
                  children: {
                    text: newlist_[j].text,
                    id: newlist_[j].id,
                    filepath: newlist_[j].FILEPATH
                  }
                }
                keyss.push(obj)
              }
            }
          }
          console.log(keyss)
          var map = {}
          var dest = []
          for (var q = 0; q < keyss.length; q++) {
            var ai = keyss[q]
            if (!map[ai.text]) {
              dest.push({
                text: ai.text,
                children: [ai.children]
              })
              map[ai.text] = ai
            } else {
              for (var e = 0; e < dest.length; e++) {
                var dj = dest[e]
                if (dj.text === ai.text) {
                  dj.children.push(ai.children)
                  break
                }
              }
            }
          }
          this.item = dest
        }
      })
    },
    onNavClick (index) {
      this.mainActiveIndex = index
    },
    onItemClick (data) {
      this.activeId = data.id
      console.log(this.activeId)
      window.location.href = data.filepath
    },
    info (item) {
      console.log(item.id)
      // this.$router.push({
      //   name: 'news_detail',
      //   query: {
      //     'id': item.id
      //   }
      // })
    },
    onSearch () {
      console.log('search')
      console.log(this.value)
      this.init(this.value)
    }
  },

  mounted () {
    this.init()
  }
}
</script>

<style src="./index.css" scoped>
</style>
